{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting Gaussians\n",
    "\n",
    "In this exercise, you'll use Python to calculate the Gaussian probability density function and then plot the results.\n",
    "\n",
    "Besides matplotlib, the exercise also uses a Python library called numpy. Numpy, [http://www.numpy.org/](http://www.numpy.org/) makes it much easier to work with arrays and matrices in Python. \n",
    "\n",
    "This exercise does not focus on numpy and how to use it. But we'll provide enough context so that you can use it in your code."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise 1\n",
    "\n",
    "Write a function for calculating the probability density function of a Gaussian. The function has three inputs and one output: \n",
    "\n",
    "**inputs**\n",
    "* mu, which is the average\n",
    "* sigma, which is the standard deviation\n",
    "* a list of x values\n",
    "\n",
    "**outputs**\n",
    "* probability density function output"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As a reminder, here is the probability density function for a Gaussian distribution:\n",
    "$$f(x) = \\frac{1}{\\sqrt{2\\pi\\sigma{^2}}}e^{\\frac{-(x-\\mu)^2}{2\\sigma^2}}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Using numpy\n",
    "To calculate the square, square root or an exponent in Python, you could use the math library; however, instead you are going to use the numpy library. Study the code below to compare the math library and the numpy library and then run the code cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "import numpy as np\n",
    "\n",
    "print('\\nExample of taking the square')\n",
    "print(math.pow(2,2))\n",
    "print(np.square(2))\n",
    "\n",
    "print('\\nExample of taking the cube')\n",
    "print(math.pow(2,3))\n",
    "print(np.power(2,3))\n",
    "\n",
    "print('\\nExample of taking the square root')\n",
    "print(math.sqrt(4))\n",
    "print(np.sqrt(4))\n",
    "\n",
    "print('\\nExample of taking the exponent')\n",
    "print(math.exp(3))\n",
    "print(np.exp(3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Using numpy with lists\n",
    "\n",
    "The numpy library lets you run mathematical expressions on elements of a list. The math library cannot do this. Study the examples below and then run the code cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('\\nExample of squaring elements in a list')\n",
    "print(np.square([1, 2, 3, 4, 5]))\n",
    "\n",
    "print('\\nExample of taking the square root of a list')\n",
    "print(np.sqrt([1, 4, 9, 16, 25]))\n",
    "\n",
    "print('\\nExamples of taking the cube of a list')\n",
    "print(np.power([1, 2, 3, 4, 5], 3))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Using numpy in a function\n",
    "\n",
    "Here is one last code example before you write your code. The example shows how to use numpy in a function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def numpy_example(x):\n",
    "    return np.exp(x)\n",
    "\n",
    "x = [1, 2, 3, 4, 5]\n",
    "print(numpy_example(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Write your code below\n",
    "Now, write the code for the probability density function. Besides the numpy sqrt, power, and exp methods, you might also want to use the np.pi method, which outputs the value for pi."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gaussian_density(x, mu, sigma):\n",
    "    # TODO: Return the probability density function for the\n",
    "    # Gaussian distribution. \n",
    "    return "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Read through and run the code cell below to check your results. We've also provided a solution in the next lesson node titled \"Plotting Gaussians in Python[Solution]\".\n",
    "\n",
    "In the code cell below, we've used the numpy linspace method, which has three inputs. The linspace method  essentially creates a list of values. In the example below, `np.linspace(0, 100, 11)` creates a list of values from 0 to 100 with 11 elements. In other words (0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100). See the [linspace documentation](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.linspace.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run this code sell to check your results\n",
    "\n",
    "# numpy linespace creates a list of values called an array\n",
    "x = np.linspace(0, 100, 11)\n",
    "\n",
    "### Expected Answer when running the code cell\n",
    "# array([  1.48671951e-07,   1.33830226e-05,   4.43184841e-04,\n",
    "#         5.39909665e-03,   2.41970725e-02,   3.98942280e-02,\n",
    "#         2.41970725e-02,   5.39909665e-03,   4.43184841e-04,\n",
    "#         1.33830226e-05,   1.48671951e-07])\n",
    "###\n",
    "gaussian_density(x, 50, 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We've also put solution code in the next part of the lesson \"Plotting Gaussians in Python [Solutions]\"\n",
    "\n",
    "# Exercise 2\n",
    "\n",
    "Write a function called plot_gaussian that creates a plot of a Gaussian function. \n",
    "\n",
    "In the programming probability exercises, we gave examples about how to make plots in Python. We'll give some guidelines here, but you might need to go back to those exercises and study the examples. \n",
    "\n",
    "Or alternatively, read through the [matplotlib documentation](https://matplotlib.org/xkcd/users/pyplot_tutorial.html).\n",
    "\n",
    "Here are the function inputs and outputs:\n",
    "\n",
    "**Inputs**\n",
    "\n",
    "* x - a numpy linespace array \n",
    "* mu - an average value\n",
    "* sigma - a standard deviation\n",
    "\n",
    "**Ouputs**\n",
    "\n",
    "This function does not need a return statement; the function will print out a visualization."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def plot_gaussian(x, mu, sigma):\n",
    "    # TODO: Use x, mu and sigma to calculate the probability density\n",
    "    # function. Put the results in the y variable.\n",
    "    # You can use your gaussian_density() function \n",
    "    # from the first exercise.\n",
    "        \n",
    "    # TODO: Plot the results in a line chart. See the first example\n",
    "    # in the pyplot tutorial for help: \n",
    "    # https://matplotlib.org/xkcd/users/pyplot_tutorial.html\n",
    "    \n",
    "    # Make sure to label the x axis, y axis and give the chart\n",
    "    # a title.\n",
    "    return None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the code below to see the results. Your visualization should look like this:\n",
    "\n",
    "![Gaussian results](gaussian_density_results.png)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run this code cell to see the results\n",
    "\n",
    "x = np.linspace(0, 100, 200)\n",
    "plot_gaussian(x, 50, 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "See the next part of the lesson for solutions to the coding exercises.\n",
    "\n",
    "In the next part of the lesson, we're going to talk about how to calculate probabilities from the probability density functions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
